package com.sgu6.redis.utils;

import android.util.Log;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


public class JedisUtil {

    protected static final String TAG = JedisUtil.class.getSimpleName();

    private JedisPool jedisPool;


    /**
     * 获取Jedis对象
     *
     * @return
     */

    public synchronized Jedis getJedis() {

        Jedis jedis = null;

        if (jedisPool != null) {
            try {
                jedis = jedisPool.getResource();
            } catch (Exception e) {
                Log.i(TAG, "getJedis: " + e.getMessage());
            }
        }
        return jedis;
    }


    /**
     * 回收Jedis对象资源
     *
     * @param jedis
     */
    @SuppressWarnings("unchecked")
    public synchronized void returnResource(Jedis jedis) {

        if (jedis != null) {

            jedisPool.returnResource(jedis);

        }

    }


    /**
     * Jedis对象出异常的时候，回收Jedis对象资源
     *
     * @param jedis
     */

    @SuppressWarnings("unchecked")
    public synchronized void returnBrokenResource(Jedis jedis) {

        if (jedis != null) {

            jedisPool.returnBrokenResource(jedis);

        }


    }

}


